#pragma once
#include <iostream>
#include <string>
#include <vector>
#include <unordered_map>
#include <fstream>
#include <mutex>
#include "Util.hpp"

namespace ns_index
{
    // 文档内容的信息
    struct DocInfo
    {
        std::string title;
        std::string content;
        std::string url;
        uint64_t id;
    };

    // 建立倒排时，倒排拉链里面的内容
    // 根据关键字得到文档id
    struct InvertedElem
    {
        uint64_t doc_id;
        std::string word;
        int weight;
        InvertedElem() : weight(0) {}
    };

    // 定义倒排拉链类型
    typedef std::vector<InvertedElem> InvertedList;

    class Index
    {
    private:
        // 正排索引，使用数组表示
        std::vector<DocInfo>forward_index;
        // 倒排索引，一个关键字对应一组InvertedElem
        std::unordered_map<std::string, InvertedList> inverted_index;
        static Index *instance;
        static std::mutex mtx;

    public:
        ~Index() {}

    private:
        Index() {}
        Index(const Index &) = delete;
        Index &operator=(const Index &) = delete;

    public:
        // 获取单例
        static Index *GetInstance()
        {
            if (instance == nullptr)
            {
                mtx.lock();
                instance = new Index();
                mtx.unlock();
            }

            return instance;
        }

        // 根据文档id找到文档内容：正排索引
        DocInfo *GetForwardIndex(uint64_t doc_id)
        {
            if (doc_id > forward_index.size())
            {
                std::cerr << "doc_id > forward.size()" << std::endl;
                return nullptr;
            }

            return &forward_index[doc_id];
        }

        // 根据关键字找到倒排拉链
        InvertedList *GetInvertedList(std::string word)
        {
            if (inverted_index.count(word) == 0)
            {
                std::cout << word << "not have InvertedList" << std::endl;
                return nullptr;
            }

            return &inverted_index[word];
        }

        // 建立索引，将文件data/input/raw/raw.html读出并建立索引
        bool BuildIndex(const std::string &input)
        {
            
            std::ifstream in(input, std::ios::in | std::ios::binary);
           
            if (!in.is_open())
            {
                std::cerr << input << "open err" << std::endl;
                return false;
            }

            

            // 建立正排索引
            std::string line;
            while (std::getline(in, line))
            {
                DocInfo *doc = BuildForwardIndex(line);
                if (doc == nullptr)
                {
                    continue;
                }

          

                // 建立倒排索引
                BuildInvertedIndex(*doc);

                 
            }

            return true;
        }

        // 建立正排索引
        DocInfo *BuildForwardIndex(std::string &line)
        {
            DocInfo doc;
            // 将文档内容进行切割:使用boost库中的函数，split
            std::vector<std::string> ret; // 接收结果
            const std::string SEP = "\3";
            ns_util::StringUtil::CutString(line, ret, SEP);
            if (ret.size() < 3)
            {
                std::cerr << "CutString err" << std::endl;
            }

            doc.title = ret[0];
            doc.content = ret[1];
            doc.url = ret[2];
            doc.id = forward_index.size();

            forward_index.push_back(doc);

            return &forward_index.back();
        }

        // 建立倒排索引
        bool BuildInvertedIndex(DocInfo &doc)
        {
            // 1,使用jieba库进行分词
            // 对doc.title进行分词
            // 对doc.content进行分词

          
            std::vector<std::string> titles_words;
            
            std::string s;
            ns_util::Ju::CutString(doc.title, titles_words);
            

            std::vector<std::string> contend_words;
            ns_util::Ju::CutString(doc.content, contend_words);
            

            // 2，统计词频

            struct word_cnt
            {
                int title_cnt;
                int contend_cnt;
                word_cnt() : title_cnt(0), contend_cnt(0)
                {
                }
            };
 

            std::unordered_map<std::string, word_cnt> word_map;
            for (auto e : titles_words)
            {
                word_map[e].title_cnt++;
            }
            

            for (auto e : contend_words)
            {
                word_map[e].contend_cnt++;
            }
    

#define X 10
#define Y 1
            for (auto word_pair : word_map)
            {
                InvertedElem e;
                e.doc_id = doc.id;
                e.word = word_pair.first;
                // 3，自定义相关性
                e.weight = X * word_pair.second.title_cnt + Y * word_pair.second.contend_cnt;
                // 4，塞入到对应的倒排表中
                InvertedList &E = inverted_index[word_pair.first];
                E.push_back(std::move(e));
            }

            

            return true;
        }
    };

    Index *Index::instance = nullptr;
    std::mutex Index::mtx;
}
